﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Main_Data;
using System.IO;
using DevExpress.XtraReports.UI;

namespace Print_UI
{
    public partial class FrmCopyTemplateUser : CommonForm
    {

        public FrmCopyTemplateUser()
        {
            InitializeComponent();
        }
        private int _id = -1;
        private int _id1 = -1;
      //  private dsReport _ds;

        private bool CheckInput()
        {
            bool result = true;
            dxErrorProvider1.ClearErrors();
            if (int.Parse(cboSemester.EditValue.ToString()) == -1 && int.Parse(cboYear.EditValue.ToString()) == -1 && txtTemp.Text == string.Empty)
            {
                dxErrorProvider1.SetError(cboYear, "Chưa Chọn Giá Trị");
                dxErrorProvider1.SetError(cboSemester, "Chưa Chọn Giá Trị");
                dxErrorProvider1.SetError(txtTemp, "Chưa Nhập Giá Trị");
                result = false;
            }
            else
            {
                if (txtTemp.Text == string.Empty)
                {
                    dxErrorProvider1.SetError(txtTemp, "Chưa Nhập Giá Trị");
                    result = false;
                }
                if (int.Parse(cboSemester.EditValue.ToString()) == -1 || int.Parse(cboSemester.EditValue.ToString()) == 0)
                {
                    dxErrorProvider1.SetError(cboSemester, "Chưa Chọn Giá Trị");
                    result = false;
                }
                if (int.Parse(cboYear.EditValue.ToString()) == -1 || int.Parse(cboYear.EditValue.ToString()) == 0)
                {
                    dxErrorProvider1.SetError(cboYear, "Chưa Chọn Giá Trị");
                    result = false;
                }

                
            }
            return result;
        }

        private void LoadDataChung()
        {

            var query = from c in Model.PRI_Template
                        where c.IsPublic == true && c.DepartmentID == null

                        select new
                        {
                            c.ID,
                            c.YearID,
                            c.SemesterID,
                            c.DepartmentID,
                            c.TemplateName,
                            c.IsPublic,
                            //c.Stream,
                            //c.HTML,
                            c.CreatedBy,
                            c.CreatedDate,
                            c.ModifiedBy,
                            c.ModifiedDate
                        };
            gvDataChung.DataSource = query;
        }

        private void LoadDataKhoa()
        {

            var query = from c in Model.PRI_Template
                        where DepartmentID == c.DepartmentID 
                        select new
                        {
                            c.ID,
                            c.CAT_Year.Name,
                            c.SemesterID,
                            c.DepartmentID,
                            c.TemplateName,
                            c.IsPublic,
                            //c.Stream,
                            //c.HTML,
                            c.CreatedBy,
                            c.CreatedDate,
                            c.ModifiedBy,
                            c.ModifiedDate
                        };
            gvDataKhoa.DataSource = query;
        }
        private void LoadSchoolYear()
        {

            var Year = (from p in Model.CAT_Year
                        join k in Model.PRI_SemesterSetting on p.ID equals k.YearID
                        //where k.IsUsed==true
                        select new { p.ID, p.Name }).Distinct();
            Dictionary<int, string> dic = new Dictionary<int, string>();
            
            dic.Add(-1, "----Chọn Năm Học----");
            foreach (var item in Year)
            {
                dic.Add(item.ID, item.Name);
            }

            cboYear.Properties.ValueMember = "Key";
            cboYear.Properties.DisplayMember = "Value";
            cboYear.Properties.DataSource = dic;
            cboYear.EditValue = -1;
        }
        //Load Semester
        private void LoadSemester()
        {

            Dictionary<int, string> dic = new Dictionary<int, string>();
            int iYear = Convert.ToInt32(cboYear.EditValue);
            var Semester = from obj in Model.PRI_SemesterSetting
                           where (obj.YearID == iYear )
                           select new
                           {
                               obj.CAT_Semester.ID,
                               obj.CAT_Semester.SemesterName
                           };
         
            dic.Add(-1, "----Chọn Học Kì----");
            foreach (var item in Semester)
            {
                dic.Add(item.ID, item.SemesterName);

            }
            cboSemester.Properties.ValueMember = "Key";
            cboSemester.Properties.DisplayMember = "Value";
            cboSemester.Properties.DataSource = dic;
            cboSemester.EditValue = -1;

        }

        private void FrmCopyTemplateUser_Load(object sender, EventArgs e)
        {
            //stcMain.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Panel1;
            LoadSchoolYear();
           // LoadSemester();
            LoadDataChung();
            LoadDataKhoa();
            
        }
        private void CopyChung()
        {
            object cellValue = gridView1.GetFocusedRowCellValue("ID");
            if (cellValue != null)
            {
                dxErrorProvider1.ClearErrors();
                stcMain.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Both;
                _id = Convert.ToInt32(cellValue);
                var obj = Model.PRI_Template.FirstOrDefault(c => c.ID == _id);
                if (obj != null)
                {
                    cboYear.EditValue = -1;
                    cboSemester.EditValue = -1;
                    txtTemp.EditValue = obj.TemplateName;
                   
                }

            }
        }
        private void CopyKhoa()
        {
            object cellValue = gridView2.GetFocusedRowCellValue("ID");
            if (cellValue != null)
            {
                dxErrorProvider1.ClearErrors();
                stcMain.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Both;
                _id1 = Convert.ToInt32(cellValue);
                var obj = Model.PRI_Template.FirstOrDefault(c => c.ID == _id1);
                if (obj != null)
                {
                    cboYear.EditValue = obj.YearID;
                    cboSemester.EditValue = obj.SemesterID;
                    txtTemp.EditValue = obj.TemplateName;
                     
                }

            }
        }
        private void btnCopy_Click(object sender, EventArgs e)
        {
            try
            {
                if (CheckInput())
                {
                   
                    if (_id != -1 && _id1 == -1)
                    {
                        int id = Convert.ToInt32(_id);
                        var template = Model.PRI_Template.FirstOrDefault(c => c.ID == id);
                        var Stream = Model.PRI_Template.FirstOrDefault(c => c.ID == -1);
                        if (template != null)
                        {
                            Stream = new Main_Data.PRI_Template();
                            Stream.ID = -1;
                            Stream.CreatedDate = DateTime.Now;
                            Stream.CreatedBy = UserName;
                            Stream.DepartmentID = DepartmentID;
                            Stream.TemplateName = txtTemp.EditValue.ToString();
                            Stream.IsPublic = false;
                            Stream.Stream = template.Stream;
                            Stream.YearID = Convert.ToInt32(cboYear.EditValue);
                            Stream.SemesterID = Convert.ToInt32(cboSemester.EditValue);
                            Model.AddToPRI_Template(Stream);
                            Model.SaveChanges();
                            CommonMessage("Copy mẫu thư thành công");
                        }

                    }
                    if (_id1 != -1 && _id == -1)
                    {
                        int id1 = Convert.ToInt32(_id1);
                        var template1 = Model.PRI_Template.FirstOrDefault(c => c.ID == id1);
                        var Stream = Model.PRI_Template.FirstOrDefault(c => c.ID == -1);
                        if (template1 != null)
                        {
                            Stream = new Main_Data.PRI_Template();
                            Stream.ID = -1;
                            Stream.CreatedDate = DateTime.Now;
                            Stream.CreatedBy = UserName;
                            Stream.DepartmentID = DepartmentID;
                            Stream.TemplateName = txtTemp.EditValue.ToString();
                            Stream.IsPublic = false;
                            Stream.Stream = template1.Stream;
                            Stream.YearID = Convert.ToInt32(cboYear.EditValue);
                            Stream.SemesterID = Convert.ToInt32(cboSemester.EditValue);

                            Model.AddToPRI_Template(Stream);
                            Model.SaveChanges();
                            CommonMessage("Copy mẫu thư thành công");
                        }

                    }
                    if (_id == -1 && _id1 == -1)
                    {
                        CommonMessage("Chưa chọn mẫu thư copy");
                    }
                    LoadDataChung();
                    LoadDataKhoa();
                }
                _id = -1;
                _id1 = -1;
                cboSemester.EditValue = -1;
                cboYear.EditValue = -1;
                txtTemp.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                DevExpress.XtraGrid.Views.Grid.GridView view = (DevExpress.XtraGrid.Views.Grid.GridView)sender;
                Point pt = view.GridControl.PointToClient(Control.MousePosition);
                DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo info = view.CalcHitInfo(pt);
                checkused();
                if (info.InRow || info.InRowCell)
                {
                    CopyChung();
                }
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void gridView2_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                DevExpress.XtraGrid.Views.Grid.GridView view = (DevExpress.XtraGrid.Views.Grid.GridView)sender;
                Point pt = view.GridControl.PointToClient(Control.MousePosition);
                DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo info = view.CalcHitInfo(pt);
                checkused();
                if (info.InRow || info.InRowCell)
                {
                    CopyKhoa();
                }
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void cboYear_EditValueChanged(object sender, EventArgs e)
        {
            LoadSemester();
            checkused();

        }
        private void checkused()
        {
            int year = Convert.ToInt32(cboYear.EditValue);
            int semester = Convert.ToInt32(cboSemester.EditValue);
            bool check = Model.PRI_SemesterSetting.Where(c => c.YearID == year && c.SemesterID == semester && c.IsUsed == true).Count() > 0;
            if (check)
            {
                btnCopy.Enabled = true;
            }
            else
            {
                btnCopy.Enabled = false;
            }
        }

        private void cboSemester_EditValueChanged(object sender, EventArgs e)
        {
            checkused();
        }
    }
}
